package service

import (
	"MyBlogv2/common/HttpCode"
	define "MyBlogv2/common/define"
	"MyBlogv2/common/models"
	"MyBlogv2/common/response"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GetLeaveWordList(c *gin.Context) {
	idStr := c.Query("id")
	var leaveWordVoList []models.LeaveWordVo
	db := models.Db.Model(new(models.LeaveWord)).
		Table("t_leave_word lw").
		Select("lw.*,u.avatar,u.nickname").
		Joins("left join sys_user u on lw.user_id = u.id")
	if idStr != "" {
		id, _ := strconv.Atoi(idStr)
		db.Where("lw.id = ?", id)
	}

	db.Where("is_check = ?", define.IS_CHECK_YES).
		Find(&leaveWordVoList)
	for i, v := range leaveWordVoList {
		var commentCount int64
		models.Db.Model(new(models.Comment)).Where("type = ? and type_id = ?", define.COMMENT_TYPE_LEAVE_WORD, v.Id).Count(&commentCount)
		leaveWordVoList[i].CommentCount = int(commentCount)
		var likeCount int64
		models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", define.LIKE_TYPE_LEAVE_WORD, v.Id).Count(&likeCount)
		leaveWordVoList[i].LikeCount = int(likeCount)
		var favoriteCount int64
		models.Db.Model(new(models.Favorite)).Where("type = ? and type_id = ?", define.FAVORITE_TYPE_LEAVE_WORD, v.Id).Count(&favoriteCount)
		leaveWordVoList[i].FavoriteCount = int(favoriteCount)

	}
	response.ResponseOK(c, leaveWordVoList)
}

func AddLeaveWord(c *gin.Context) {
	var leaveWord models.LeaveWord
	var content string
	userId, _ := c.Get("userId")
	if err := c.ShouldBind(&content); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if len(content) > define.LEAVE_WORD_CONTENT_LENGTH {
		response.ResponseErrorWithMsg(c, HttpCode.PARAM_ERROR, "留言内容不能超过"+strconv.Itoa(define.LEAVE_WORD_CONTENT_LENGTH)+"个字符")
		return
	}
	leaveWord.Content = content
	leaveWord.UserId = userId.(int)
	err := models.Db.Model(new(models.LeaveWord)).Create(&leaveWord).Error
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	response.ResponseOK(c, nil)
}
func GetLeaveWordList4Admin(c *gin.Context) {
	var leaveWordVoList []models.LeaveWordListVo
	models.Db.Model(new(models.LeaveWordListVo)).
		Table("t_leave_word lw").
		Select("lw.*,u.username as username").
		Joins("left join sys_user u on lw.user_id = u.id").
		Find(&leaveWordVoList)
	response.ResponseOK(c, leaveWordVoList)
}

func SearchLeaveWord(c *gin.Context) {
	var leaveWordVoList []models.LeaveWordListVo
	var searchLeaveWordDto models.SearchLeaveWordDto
	if err := c.ShouldBind(&searchLeaveWordDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.LeaveWordListVo)).
		Table("t_leave_word lw").
		Select("lw.*,u.username as username").
		Joins("left join sys_user u on lw.user_id = u.id")
	if searchLeaveWordDto.Username != "" {
		db.Where("u.username like ?", "%"+searchLeaveWordDto.Username+"%")
	}
	if !time.Time(searchLeaveWordDto.StartTime).IsZero() {
		db.Where("lw.created_at >= ?", searchLeaveWordDto.StartTime)
	}
	if !time.Time(searchLeaveWordDto.EndTime).IsZero() {
		db.Where("lw.created_at <= ?", searchLeaveWordDto.EndTime)
	}
	if searchLeaveWordDto.IsCheck.Valid {
		db.Where("lw.is_check = ?", searchLeaveWordDto.IsCheck.Int64)
	}
	db.Find(&leaveWordVoList)
	response.ResponseOK(c, leaveWordVoList)
}

func UpdateLeaveWordIsCheck(c *gin.Context) {
	var leaveWordIsCheckDto models.LeaveWordIsCheckDto
	if err := c.ShouldBind(&leaveWordIsCheckDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.LeaveWord)).Where("id = ?", leaveWordIsCheckDto.Id).Update("is_check", leaveWordIsCheckDto.IsCheck)
	response.ResponseOK(c, nil)
}
func DeleteLeaveWord(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.LeaveWord)).Where("id in (?)", ids).Delete(new(models.LeaveWord))
	models.Db.Model(new(models.Comment)).Where("type = ?", define.COMMENT_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Comment))
	models.Db.Model(new(models.Like)).Where("type = ?", define.LIKE_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Like))
	models.Db.Model(new(models.Favorite)).Where("type = ?", define.FAVORITE_TYPE_LEAVE_WORD).Where("type_id in (?)", ids).Delete(new(models.Favorite))
	response.ResponseOK(c, nil)
}
